package com.yellowpage.white.common;

import javax.servlet.http.HttpServletRequest;

/**
 * White框架 查询列表的分页显礄1�7
 * @author jamzhou
 *
 */

public class PageRender {
	private int PAGE_SIZE = 10;	

	private String htmlStr;

	private int startNum;
	private int endNum;
	private int perPageNum;

	HttpServletRequest request = null;

	String filename = ""; // 文件各1�7
	int page = 1; // 页号
	int totals = -1; // 记录总数
	int perpagenum = 20; // 每页显示记录敄1�7	
	int allpage = 1; // 总页敄1�7
	int cpage = 1; // 当前顄1�7
	int spage = 1; // 弄1�7始记录数

	String listPageBreak = "";
	String[] pagesign = null;

	/**
	 * 通用环境下的使用
	 * 
	 * @param startNum
	 * @param endNum
	 */
	public PageRender(int startNum, int endNum) {
		this.startNum = startNum;
		this.endNum = endNum;
		this.perPageNum = endNum - startNum;
	}

	/**
	 * 提供在HttpServlet环境下的使用
	 * 
	 * @param request
	 * @param totalNum
	 */
	public PageRender(HttpServletRequest request, int totalNum) {

		int cpage = 1;
		if (request.getParameter("byPage") != null
				&& request.getParameter("byPage").equals("true")) {
			if (request.getParameter("page") != null
					&& !request.getParameter("page").equals("")) {
				cpage = Integer.parseInt(request.getParameter("page"));
			}
		}

		this.setPage(cpage);

		String perPageNum = request.getParameter("perPageNum");
		if (perPageNum != null && perPageNum.trim().length() > 0) {
			this.setPerPageNum(Integer.parseInt(perPageNum.trim()));
		} else {
			this.setPerPageNum(PAGE_SIZE);
		}

		this.setTotals(totalNum);		
		this.doPageBreak();

		request.setAttribute("pageRender", this);
	}
	
	
	public PageRender(HttpServletRequest request) {

		int cpage = 1;		
		
		if (request.getParameter("byPage") != null
				&& request.getParameter("byPage").equals("true")) {
			if (request.getParameter("page") != null
					&& !request.getParameter("page").equals("")) {
				cpage = Integer.parseInt(request.getParameter("page"));
			}			
		}

		this.setPage(cpage);

		String perPageNum = request.getParameter("perPageNum");
		if (perPageNum != null && perPageNum.trim().length() > 0) {
			this.setPerPageNum(Integer.parseInt(perPageNum.trim()));
		} else {
			this.setPerPageNum(PAGE_SIZE);
		}					
	}	
	
	public int getStartNum() {
		return this.getSpage();
	}

	public int getEndNum() {
		return this.getSpage() + this.getPerPageNum();
	}

	

	public String getHtmlStr() {
		return htmlStr;
	}

	public void setHtmlStr(String htmlStr) {
		this.htmlStr = htmlStr;
	}

	public void doPageBreak() {
		this.allpage = (int) Math.ceil((this.totals + this.perpagenum - 1)
				/ this.perpagenum);
		int intPage = this.page;
		if (intPage > this.allpage) { // pages == 0
			this.cpage = 1;
		} else {
			this.cpage = intPage;
		}
		this.spage = (this.cpage - 1) * this.perpagenum;

		getPageBreakStrByForm();

	}
	
	public void doPageBreakNoCount() {
		this.allpage = this.page + 1;
		int intPage = this.page;
		if (intPage > this.allpage) { // pages == 0
			this.cpage = 1;
		} else {
			this.cpage = intPage;
		}
		this.spage = (this.cpage - 1) * this.perpagenum;

		getPageBreakStrByForm();

	}

	public String getListPageBreak() {
		return this.listPageBreak;
	}

	/* 由formbean传入获取换页参数 */
	private void getPageBreakStrByForm() {
		// System.out.println("....form....."+style);
		if (this.filename.indexOf("?") == -1 && (this.filename.endsWith(".do"))) {
			this.filename = this.filename + "?";
		} else {
			if (!this.filename.endsWith("&")) {
				this.filename = this.filename + "&";
			}
		}

		StringBuffer sb = new StringBuffer();

		if (this.filename != null && this.filename.length() > 0) {
			sb.append("<table width='0' border='0' cellspacing='0' cellpadding='0'>\n");
			sb.append("<tr valign=middle >\n");
			sb.append("<td width='61'>&nbsp;</td>\n");
			sb.append("<td width='47'><img src='images/pagination/g1.jpg' width='6' height='21' /></td>\n");
			sb.append("<td width='267' class='copy_1'>每页\n");
			sb.append("<input name='perPageNum' type='text' value='"
					+ perpagenum + "' size='2' />\n");
			sb.append("杄1�7 &nbsp;&nbsp;\n");
			int _cpage = 1;
			if (this.allpage == 0) {
				_cpage = 1;
			} else {
				_cpage = cpage;
			}
			sb.append(getAllPageHtml(_cpage));
			sb.append("</td>\n");
			sb
					.append("<td width='10'><img src='images/pagination/g1.jpg' width='6' height='21' /></td>\n");
			sb.append("\n");

			if (this.cpage > 1) {
				sb
						.append("<td width='25'><a href='javascript:toPageQuery(1)'>");
				sb
						.append("<img src='images/pagination/g2.jpg' width='15' height='13' border='0' /></a></td>\n");
				sb.append("<td width='25'><a href='javascript:toPageQuery("
						+ (cpage - 1) + ")'>");
				sb
						.append("<img src='images/pagination/g3.jpg' width='15' height='13' border='0' /></a></td>\n");
			} else {
				sb.append("<td width='25'>&nbsp;</td>\n");
				sb.append("<td width='25'>&nbsp;</td>\n");
			}
			if (this.cpage < this.allpage) {
				sb.append("<td width='25'><a href='javascript:toPageQuery("
						+ (cpage + 1) + ")'>");
				sb
						.append("<img src='images/pagination/g4.jpg' width='15' height='13' border='0' /></a></td>\n");
				sb.append("<td width='25'><a href='javascript:toPageQuery("
						+ allpage + ")'>");
				sb
						.append("<img src='images/pagination/g5.jpg' width='15' height='13' border='0' /></a></td>\n");
			} else {
				sb.append("<td width='25'>&nbsp;</td>\n");
				sb.append("<td width='25'>&nbsp;</td>\n");
			}
			sb.append("<td width='133' class='copy_1'>&nbsp;</td>\n");
			sb.append("<td width='97'><span class='copy_1'>转到笄1�7");
			sb.append("<input name='page' type='text' value='" + _cpage
					+ "' size='2' />\n");
			sb.append("顄1�7/span></td>\n");
			sb
					.append("<td width='24'><a href='javascript:toSpecifiedPageQuery("
							+ allpage
							+ ")'><img src='images/pagination/g6.jpg' width='24' height='22' border='0' /></a></td>\n");
			sb.append("<td width='67'>&nbsp;</td>\n");
			sb.append("</tr>\n");
			sb.append("</table>\n");
			this.listPageBreak = sb.toString();
		}
		return;

	}

	public String getAllPageHtml(int _cpage) {
		StringBuffer sb = new StringBuffer(16);
		sb.append("兄1�7 ");
		sb.append(totals);
		sb.append(" 杄1�7 ");
		sb.append("笄1�7 ");
		sb.append(_cpage);
		sb.append("/");
		sb.append(allpage);
		sb.append(" 顄1�7");
		return sb.toString();
	}

	public int getAllpage() {
		return allpage;
	}

	public void setAllpage(int allpage) {
		this.allpage = allpage;
	}

	public String getToPageHtml() {
		return "刄1�7 <input name=page type=text size=2 class=INPUT1> 顄1�7 <input type=submit name=Submit class=BUTTN-FIX1 value=GO>";
	}

	public String toPageHtml() {
		StringBuffer sb = new StringBuffer();
		sb.append("<input name='转到' type='button' onClick='toPageQuery()'>");
		sb.append("<input name='toPage' type='text' style='width:12px'>顄1�7");
		return sb.toString();
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getFileName() {
		return this.filename;
	}

	public void setFileName(String aFileName) {
		this.filename = aFileName;
	}

	public int getPage() {
		return this.page;
	}

	public void setPage(int aPage) {
		this.page = aPage;
	}

	public int getTotals() {
		return this.totals;
	}

	public void setTotals(int aTotals) {
		this.totals = aTotals;
	}

	public int getPerPageNum() {
		return this.perpagenum;
	}

	public void setPerPageNum(int aperpagenum) {
		this.perpagenum = aperpagenum;
	}
	

	public void setPagesign(String[] apagesign) {
		this.pagesign = apagesign;
	}

	public int getSpage() {
		return this.spage;
	}

}
